package com.changgou.oauth.controller;

import com.changgou.oauth.service.UserLoginService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/oauth")
public class userLoginController {

    //客户端id
    @Value("${auth.clientId}")
    private String clientId;

    //客户端密钥
    @Value("${auth.clientSecret}")
    private String clientSecret;

    @Autowired
    private UserLoginService userLoginService;

    /**
     * 参数传递:
     * 1.账号 username=szithe ima
     * 2.密码 password=szitheima
     * 3.授权方式 grant_type=password
     * 请求头传递
     * 4. Basic Base64(客户端ID:客户端秘钥) Authorizat ion=Basic Y2hhbmdnb3U6Y2hhbmdnb3U=
     */
    @RequestMapping("/login")
    public Result login(String username,String password) throws Exception {
        String grant_type = "password";
        ResponseEntity<Map> entity = userLoginService.login(username, password, grant_type, clientId, clientSecret);
        if (entity != null){
            return new Result(true, StatusCode.OK,"登录成功！",entity);
        }
        return new Result(false, StatusCode.LOGINERROR,"登录失败！",entity);
    }

}
